<template>
  <div class="main">
    <!-- 文章列表table表单 -->
    <SkuDetail ref="SkuDetail" class="array" />
    <el-card shadow="never" class="card">
      <div slot="header" class="clearfix">
        <el-button type="primary" size="default" @click="add"
          >新增角色</el-button
        >
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
        max-height="1000"
        v-loading="loading"
        border
      >
        <el-table-column label="#" fixed type="index" width="50">
        </el-table-column>
        <el-table-column
          prop="roleName"
          label="角色名称"
          width="200"
          heigth="133"
          align="center"
        >
        </el-table-column>

        <el-table-column
          prop="roleCode"
          label="角色编码"
          width="300"
          align="center"
        >
        </el-table-column>

        <el-table-column prop="remark" label="备注" width="600" align="center">
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="添加时间"
          width="300"
          align="center"
        >
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              @click="edit(scope.row)"
              type="text"
              size="small"
              style="color: aqua !important"
            >
              编辑
            </el-button>
            <el-button
              @click="deleteRow(scope.row)"
              type="text"
              size="small"
              style="color: red !important"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页 -->
    <el-pagination
      :current-page="page.currentPage"
      :page-sizes="page.pageSizes"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>

<script>
import authority from '@/api/authority.js'
import mixin from '@/mixins'
import SkuDetail from './components/SkuDetail.vue'
export default {
  mixins: [mixin],
  components: {
    SkuDetail,
  },
  data() {
    return {
      searchFrom: {}, //输入框里的对象
      form: {}, //实际搜索要用的表单
      tableData: [], //文章列表数据
      loading: false,

      value: '',
    }
  },
  created() {
    this.findRolesByPage()
  },
  mounted() {},
  methods: {
    //编辑文章
    edit(row) {
      console.log(row, '编辑')
      this.$refs.SkuDetail.open(row)
    },
    //新增文章add
    add() {
      //跳转到新增文章页面
      console.log('新增')
      this.$refs.SkuDetail.open()
    },

    //删除文章
    deleteRow(row) {
      console.log(row, '删除')
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        authority.removeRoleApi(row.id).then(res => {
          if (res.success) {
            this.$message.success('删除成功')
            this.findRolesByPage()
          } else {
            this.$message.error(res.message)
          }
        })
      })
    },

    //获取文章列表
    findRolesByPage() {
      authority
        .findRolesByPageApi(
          this.page.currentPage,
          this.page.pageSize,
          this.from
        )
        .then(res => {
          console.log(res, 'res')
          if (res.success) {
            this.tableData = res.data.rows //获取到线上文字地址
            this.page.total = res.data.total //获取文章总条数
          } else {
            this.$message.error('请求失败')
          }
          console.log(this.tableData, 'this.tableData')
        })
    },

    //每页多少条 改变时触发事件
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.page.pageSize = val
      this.findRolesByPage()
    },
    //修改页触发的事件
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page.currentPage = val
      this.findRolesByPage()
    },
  },
}
</script>

<style lang="scss" scoped>
.main {
  background: #f5f5f5;
  .card {
    width: 96.5%;
    margin: 30px;
  }
}
.condtion {
  background-color: #fff;
  color: #303133;
  font-size: 10px;
}
.users {
  margin-right: 200px;
}
.usered {
  position: relative;
  top: 30px;
}
.el-card__body {
  height: 1200px;
}
.frame {
  color: #409eff;
  border-radius: 4px;
  background-color: #ecf5ff;
  border-color: #d9ecff;
}
</style>